package com.zhangh.summer.admin.support.spring;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.zhangh.summer.admin.support.boot.interfaces.OnAppConfigConditional;
import com.zhangh.summer.admin.support.prometheus.request.RequestInterceptor;
import com.zhangh.summer.admin.support.boot.common.AppConfigEnum;
import com.zhangh.summer.admin.support.session.SessionInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@Configuration
@OnAppConfigConditional(AppConfigEnum.WEBMVC_CONFIGURATION_ENABLE)
@Slf4j
public  class WebMvcConfiguration extends WebMvcConfigurationSupport {

    public WebMvcConfiguration(){
        log.info("load webmvc config");
    }

    /**
     * 业务方不要重写这个，请重写 addProjectInterceptors
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        addBasicInterceptors(registry);
        addProjectInterceptors(registry);
    }

    /**
     * 基础拦截器
     * @param registry
     */
    private void addBasicInterceptors(InterceptorRegistry registry){
        //trace 拦截器
        registry.addInterceptor(new SessionInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/login","/loginCheck","/getServerTime");
        //prometheus 拦截器
        registry.addInterceptor(new RequestInterceptor()).addPathPatterns("/**");
    }

    /**
     * 添加项目拦截器
     * @param registry
     */
    protected void addProjectInterceptors(InterceptorRegistry registry){

    }


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);

        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converters.add(stringHttpMessageConverter);

        FastJsonHttpMessageConverter fastJsonHttpMessageConverter=new FastJsonHttpMessageConverter();
        List<MediaType> supportMediaTypeList = new ArrayList<>();
        supportMediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
        supportMediaTypeList.add(MediaType.valueOf("application/vnd.spring-boot.actuator.v2+json"));

        FastJsonConfig config = new FastJsonConfig();
//        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        config.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue,//保留空的字段
                SerializerFeature.WriteNullStringAsEmpty,//String null -> ""
                SerializerFeature.WriteNullNumberAsZero,//Number null -> 0
                SerializerFeature.WriteNullListAsEmpty,//List null-> []
                SerializerFeature.WriteNullBooleanAsFalse);//Boolean null -> false
        fastJsonHttpMessageConverter.setFastJsonConfig(config);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportMediaTypeList);
        fastJsonHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
        converters.add(fastJsonHttpMessageConverter);

    }

    @Override
    public void configureContentNegotiation(
            ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }
}
